package com.ebelter.btcomlib.models.https;

import android.text.TextUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单的json字符长统一放在这里生成
 * 没必要单独写一个bean对象然后再gson.toJosn
 */

public class BaseJsonGenerator {

    //获取json数据{"email":"1070@qq.com","type":1}
    public String getEmailValidateRJ(String email,int type,String language){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("email",email);
        paramMap.put("type",type);
        paramMap.put("local",language);
        return getMapRJ(paramMap);
    }

    //获取json数据{"email":"1070@qq.com","type":1}
    public String getPhoneValidateRJ(String phone,int type,String language){
        Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("mobilePhone",phone);
        paramMap.put("type",type);
        paramMap.put("local",language);
        return getMapRJ(paramMap);
    }


    //获取注册的json数据
    public String getRegisterRJ(String nickName,String email,String password,String birthday,
                                       int height,int weight, int sex,int profession,String code,int type){
        return "{\"nickName\":\""+nickName+"\",\"email\":\""+email+"\",\"password\":\""+
                password+"\",\"birthday\":\""+birthday+"\",\"height\":"+height+",\"weight\":"+
                weight+",\"sex\":"+sex+",\"profession\":"+profession+ ",\"code\":\""+code+"\",\"type\":"+type+"}";
    }

    //获取注册的json数据
    public String getRegisterRJ2(Long mainId,String nickName,String email,String password,String birthday,
                                int height, int sex,int profession,int type) {
        Map<String,Serializable> paramMap=new HashMap<>();
        if (mainId>0){
            paramMap.put("mainId",mainId);
        }
        paramMap.put("nickName",nickName);
        if (!TextUtils.isEmpty(email)){
            paramMap.put("email",email);
        }

        if (!TextUtils.isEmpty(password)){
            paramMap.put("password",password);
        }

        paramMap.put("birthday",birthday);
        paramMap.put("height",height);
        paramMap.put("sex",sex);
        paramMap.put("profession",profession);
        paramMap.put("type",type);

        return getMapRJ(paramMap);

//        return "{\"nickName\":\"" + nickName + "\",\"email\":\"" + email + "\",\"password\":\"" +
//                password + "\",\"birthday\":\"" + birthday + "\",\"height\":" + height + ",\"sex\":" + sex + ",\"profession\":" + profession + ",\"type\":" + type + "}";
//
    }


    //获取登录的json
    public String getLoginRJ(boolean isLoginEmail,String loginStr,String password){

        StringBuilder sb=new StringBuilder();
        sb.append("{");
        if (!isLoginEmail){
            sb.append("\"nickName\":");
        }else {
            sb.append("\"email\":");
        }

        sb.append("\"").append(loginStr).append("\",");

        sb.append("\"password\":\"").append(password).append("\"}");
        return sb.toString();

    }

   // private boolean isEmpty(String str){
   //     return str==null||str.length()==0;
   // }


/*
    //邮箱或者昵称验证
    public String getEmailorNicknameValidateRJ(String validateStr, boolean isValidateEmail) {
        StringBuilder sb=new StringBuilder();
        sb.append("{");
        if (isValidateEmail){
            sb.append("\"email\":");
        }else {
            sb.append("\"nickName\":");
        }
        sb.append("\"").append(validateStr).append("\"}");
        return sb.toString();
    }*/

    //拼凑所有的键值对json信息
    public String getMapRJ(Map<String, Serializable> paramMap) {

        StringBuilder sb=new StringBuilder();
        sb.append("{");

        if (paramMap!=null){
            for(String key : paramMap.keySet()){
                sb.append("\"").append(key).append("\":");
                Serializable value = paramMap.get(key);
                if (value instanceof String){
                    sb.append("\"").append((String) value).append("\"");
                }else if (value instanceof Integer){
                    sb.append(value);
                }else if (value instanceof Float){
                    sb.append(value);
                }else if (value instanceof Long){
                    sb.append(value);
                }
                sb.append(",");
            }
            sb.deleteCharAt(sb.lastIndexOf(","));
        }

        sb.append("}");

        return sb.toString();
    }



}
